package com.tsu.mall.admin.exceptionhandler;

import com.tsu.mall.admin.exception.BusinessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.HashMap;
import java.util.Map;

//统一异常处理类
@ControllerAdvice
public class MyExceptionHandler {
    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity handler1(RuntimeException e){
        Map<String,String> map=new HashMap<>();
        map.put("msg","出运行时异常了");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(map);
    }

    @ExceptionHandler(BusinessException.class)
    public ResponseEntity handler2(BusinessException e){
        Map<String,String> map=new HashMap<>();
        map.put("msg",e.getMsg());
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(map);
    }


    @ExceptionHandler(Exception.class)
    public ResponseEntity handler(Exception e) {
        Map<String, String> map = new HashMap<>();
        map.put("msg", "出异常了");
       return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(map);
    }

}
